package com.infinitegroup.findmystuff.activities;

import com.infinitegroup.findmystuff.R;
import com.infinitegroup.findmystuff.presenters.RegistrationPresenter;
import com.infinitegroup.findmystuff.views.RegistrationView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 *  Handles the inputs of the register screen and gives these inputs to the register
 * controller (who actually handles adding the item to the database) and can advance to
 * the login screen.
 * 
 * @author Tri-An
 *
 */
public class RegistrationActivity extends Activity implements RegistrationView{
	
	private RegistrationPresenter presenter;
	
	/**
	 * Handles the start of the activity and sets the listeners for the buttons on the screen.
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registration);
		
		presenter = new RegistrationPresenter(this);
		
		Button b = (Button) findViewById(R.id.register_registration_button);
		
		b.setOnClickListener(new OnClickListener() {
			/**
			 * Handles the clicking of the register button.
			 */
			public void onClick(View v) {
				onRegisterClick();			
			}
		});
	}
	
	/**
	 * Destroy activity on back button press.
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
	    if ((keyCode == KeyEvent.KEYCODE_BACK)){
	        finish();
	    }
	    return super.onKeyDown(keyCode, event);
	}

	/**
	 * Advances the app to the login screen.
	 */
	public void advanceToLogin() {
		Intent intent = new Intent(this, LoginActivity.class);
		startActivity(intent);
	}

	/**
	 * Displays inputed text as a popup message.
	 */
	public void displayMessage(String string) {
		if (string!=null){
			Toast toast = Toast.makeText(this, string, Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
		}
	}

	/**
	 * Gets all of the information out of the text fields and gives it to the presenter.
	 */
	public void onRegisterClick() {
		String firstName = ((EditText) findViewById(R.id.register_firstname)).getText().toString();  
		String lastName = ((EditText) findViewById(R.id.register_lastname)).getText().toString();
		String username = ((EditText) findViewById(R.id.register_username)).getText().toString();  
		String password = ((EditText) findViewById(R.id.register_password)).getText().toString();  
		String email = ((EditText) findViewById(R.id.register_email)).getText().toString();  
		String phone = ((EditText) findViewById(R.id.register_phone)).getText().toString();  
		String zipCode = ((EditText) findViewById(R.id.register_zipcode)).getText().toString();
		presenter.handleRegisterClick(firstName, lastName, username, password, email, phone, zipCode);
	}
}
